package com.jframe.cfg.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jframe.cfg.model.DicType;

import com.jframe.cfg.service.DictypeService;
import com.jframe.sys.controller.ExtendAction;
import com.jframe.sys.controller.utils.*;
import com.jframe.sys.utils.*;
import com.jframe.sys.webui.*;

public class DictypeAction extends ExtendAction{
	
	private static String ACTION="dictypeAction";
	private static String SERVICE="dictypeService";
	private static Class<DicType> clazz=DicType.class;
	
	public void list(){
		List<Columns> frozenColumns = new ArrayList<Columns>();//固定列
		List<Columns> commonColumns = new ArrayList<Columns>();
		frozenColumns.add(new Columns("ck","",40,""));
		commonColumns.add(new Columns("id", "编号", 100,""));
		commonColumns.add(new Columns("enname", "代码", 140,""));
		commonColumns.add(new Columns("cnname","名称",140,""));
		commonColumns.add(new Columns("upId","子系统",220,new Editor("combobox",true),DicCache.getComboboxJson("sm_sys_app")));
		Grid grid = new Grid();
		grid.setUrl(this.getUrl(ACTION+".do?method=load"));
		String js = JsBuilder.getJsStr(frozenColumns, commonColumns, grid);
		this.request.setAttribute("js", js);
		this.forward("/jframe/cfg/list/dictypeList.jsp");
	}
		
	public void load(){
		this.baseLoad(SERVICE, clazz);
	}
	
	public void save(){
		String obj = this.request.getParameter("obj");
		Map<String,Object> map = new HashMap<String,Object>();
		DicType dictype = (DicType) JsonUtil.json2Obj(obj,clazz);
		DictypeService dictypeService = (DictypeService)this.getBean(SERVICE);	
		int index=StringUtil.null2Space(dictype.getEnname()).indexOf("dm_");
		if(index!=0){
			map.put("result", "数据字典代码请用'dm_'开头！");
			this.print(JsonUtil.map2Json(map));			
			return;
		}
		DicType dictype1 =dictypeService.load("enname", dictype.getEnname());
		if(!StringUtil.null2Space(dictype1.getCnname()).equals(dictype.getCnname())&&!StringUtil.null2Space(dictype1.getCnname()).equals("")){
			map.put("result", "代码重复，请重新填写！");
			this.print(JsonUtil.map2Json(map));			
			return;
		}
		dictype.setFlag(0);
		dictypeService.save(dictype);
		map.put("result", true);
		map.put("obj", dictype);
		this.print(JsonUtil.map2Json(map));
	}
	
	public void delete(){
		this.baseDelete(SERVICE);
	}
	
	public void getUpDictype(){
		int flag = StringUtil.parseInt(this.request.getParameter("flag"));
		DictypeService dictypeService = (DictypeService)this.getBean(SERVICE);	
		List<DicType> ls = dictypeService.findListByFlag(flag);
		String json = "[";
		for(DicType dictype:ls){
			json +="{'id':"+dictype.getId()+",";
			json +="'name':'"+dictype.getCnname()+"'},";
		}
		if(ls.size()>0){
			json.substring(0, json.length()-1);
		}
		json +="]";
		this.print(json);
	}
	
/*	public void getRightTrees(){
		int id = StringUtil.parseInt(this.request.getParameter("id"));
		int flag = StringUtil.parseInt(this.request.getParameter("flag"));
		DictypeService dictypeService = (DictypeService)this.getBean("dictypeService");	
		List<DicType> treels = dictypeService.findListByUpIdAndFlag(id, flag);
		List<Tree> ls = new ArrayList<Tree>();
		for(DicType dictype:treels){
			Tree tree = new Tree();
			tree.setId(dictype.getId());
			tree.setText(dictype.getCnname());
			tree.setLeaf(dictypeService.isleaf(dictype.getId(),flag));			
			ls.add(tree);
		}
		this.print(JsonUtil.list2Json(ls));
	}*/
	
	public void getObjById(){
		this.print(JsonUtil.obj2Json(this.getObject(SERVICE)));
	}
	
	public void reload(){
		int id = StringUtil.parseInt(request.getParameter("id"));
		DictypeService dictypeService = (DictypeService)this.getBean(SERVICE);				
		this.print(dictypeService.reload(id));
	}
	
	public void getCache(){
		/*CacheUtil cache = CacheUtil.getInstance("datacache");
		String json = (String)cache.get("sm_sys_menu_tree");
		System.out.println(json);
		this.print(json);*/	
		//this.log.debug(DicCache.getComboboxJson("dm_sys_active"));
		this.print(DicCache.getComboboxJson("dm_sys_active"));
	}
}
